/**
 * 
 */
package sqs.service;

import java.util.Iterator;
import java.util.List;

import javax.naming.InitialContext;

import sqs.ejb.sessionbean.QuestionnaireMgr;
import sqs.model.Answer;
import sqs.model.Option;
import sqs.model.Question;
import sqs.model.Questionnaire;
import sqs.model.SubmittedQuestionnaire;

/**
 * @author kjleng
 *
 */
public class TestSurveyResult {
	public static void main(String[] args) {
		try {
			TestSurveyResult client = new TestSurveyResult();
			client.generateReport(1);
			

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void generateReport(int qId)throws Exception {
		InitialContext ctx = new InitialContext();
		QuestionnaireMgr mgr = (QuestionnaireMgr)ctx.lookup("ejb/QuestionnaireJNDI");
//		UserMgr userMgr = (UserMgr)ctx.lookup("ejb/UserJNDI");
//		User researcher = userMgr.getUserByUserName("kjleng");
		Questionnaire qnaire = mgr.getQuestionnaireByQuestionnaireId(qId);
		
		String title = qnaire.getQuestionnaireTitle();
		String desc = qnaire.getDescription();
		List<SubmittedQuestionnaire> submittedQnaires = qnaire.getSubmittedQuestionnaires();
		int noOfRespondents = submittedQnaires.size();
		System.out.println("title = " +title);
		System.out.println("desc = " +desc);
		System.out.println("noOfRespondents = " +noOfRespondents);
		
		List<Question> questions = qnaire.getQuestions();
		for(int i=0;i<questions.size();i++){
			Question q = questions.get(i);
			System.out.println("question ID = " +q.getQuestionId());
			List<Answer> answers = q.getAnswers();
			Iterator<Answer> aIter = answers.iterator();
			while(aIter.hasNext()){
				Answer ans = aIter.next();
				System.out.println("ans ID = " +ans.getAnswerId());
				System.out.println();
				System.out.println("question num = " +i);
				System.out.println("question text = " +q.getQuestionText());
				System.out.println("num of respondents for this question = "+q.getNumOfRespondents());
				List<Option> options = ans.getOptions();
				Iterator<Option> iter = options.iterator();
				while(iter.hasNext()){
					Option option = iter.next();
					System.out.println("option text = " +option.getOptionText());
					System.out.println("rating = " +option.getRating());
					
				}
			}
			
		}
		
	}
}
